// 版权归Go作者所有。版权所有。
// 此源代码的使用受BSD样式
// 许可证的约束，该许可证可以在许可证文件中找到。

package runtime

import (
	"internal/abi"
	"unsafe"
)

// X版本的syscall期望libc调用返回64位结果。
// 否则（非X版本）需要32位结果。
// 之所以需要这种区别，是因为返回-1表示错误，
// 我们需要知道是检查结果的32位还是64位。
// （一些返回32位的libc函数将垃圾放在AX的上32位。）

// go:linkname syscall\u syscall syscall.syscall 
// go:nosplit 
// go:cgo\u unsafe\u args 
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscall()

// go:linkname syscall\u syscallX syscallX syscallX syscall.syscallX 
// go:nosplit 
func syscall_syscallX(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscallX)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscallX()

// go:linkname syscall
// go:cgo_unsafe_args 
func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall6)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscall6()

// go:linkname syscall\u syscall 6x syscall.syscall6X 
// go:nosplit 
// go cgo_unsafe_args 
func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall6X)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscall6X()

// go linkname syscallPtr syscallPtr syscall syscall 63
func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscallPtr)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscallPtr()

// go:nosplit 
// go:cgo\u unsafe\u args 
func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall)), unsafe.Pointer(&fn))
	return
}

// go:linkname syscall\u rawsystcall 6 syscall.rawsystcall6 
// go nosplit 
func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscall6)), unsafe.Pointer(&fn))
	return
}

// sysnoerr在crypto/x509中用于调用Security.framework和CF.

// go:linkname crypto\u x509\u syscall crypto/x509/internal/macos.syscall 
// go:nosplit 
// go:cgo\u unsafe\u args 
func crypto_x509_syscall(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1 uintptr) {
	entersyscall()
	libcCall(unsafe.Pointer(abi.FuncPCABI0(syscallNoErr)), unsafe.Pointer(&fn))
	exitsyscall()
	return
}
func syscallNoErr()

// u trampoline函数从go调用约定转换为C调用约定
// 然后调用底层libc函数。它们在sys_darwin_uu$ARCH.s中定义。go:cgo\u unsafe\u args 

func pthread_attr_init(attr *pthreadattr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_init_trampoline)), unsafe.Pointer(&attr))
}
func pthread_attr_init_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func pthread_attr_getstacksize(attr *pthreadattr, size *uintptr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_getstacksize_trampoline)), unsafe.Pointer(&attr))
}
func pthread_attr_getstacksize_trampoline()

// go:cgo\u unsafe\u args 
func pthread_attr_setdetachstate(attr *pthreadattr, state int) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_attr_setdetachstate_trampoline)), unsafe.Pointer(&attr))
}
func pthread_attr_setdetachstate_trampoline()

// go:nosplit 
// go:cgo:cgo\u unsafe\u args 
func pthread_create(attr *pthreadattr, start uintptr, arg unsafe.Pointer) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_create_trampoline)), unsafe.Pointer(&attr))
}
func pthread_create_trampoline()

// go
func raise(sig uint32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(raise_trampoline)), unsafe.Pointer(&sig))
}
func raise_trampoline()

// go:cgo_unsafe_args 
func pthread_self() (t pthread) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_self_trampoline)), unsafe.Pointer(&t))
	return
}
func pthread_self_trampoline()

// go:nosplit 
// go:cgo_unsafe_args 
func pthread_kill(t pthread, sig uint32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_kill_trampoline)), unsafe.Pointer(&t))
	return
}
func pthread_kill_trampoline()

// mmap用于通过mmap进行低级内存分配。不允许堆栈
// 拆分，因为此函数（由sysAlloc使用）在运行时的许多低级
// 部分调用，调用方通常认为它不会获得任何锁。go:nosplit 
func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
	args := struct {
		addr            unsafe.Pointer
		n               uintptr
		prot, flags, fd int32
		off             uint32
		ret1            unsafe.Pointer
		ret2            int
	}{addr, n, prot, flags, fd, off, nil, 0}
	libcCall(unsafe.Pointer(abi.FuncPCABI0(mmap_trampoline)), unsafe.Pointer(&args))
	return args.ret1, args.ret2
}
func mmap_trampoline()

// go:cgo\u unsafe\u args 
func munmap(addr unsafe.Pointer, n uintptr) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(munmap_trampoline)), unsafe.Pointer(&addr))
}
func munmap_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(madvise_trampoline)), unsafe.Pointer(&addr))
}
func madvise_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func mlock(addr unsafe.Pointer, n uintptr) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(mlock_trampoline)), unsafe.Pointer(&addr))
}
func mlock_trampoline()

// go
func read(fd int32, p unsafe.Pointer, n int32) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(read_trampoline)), unsafe.Pointer(&fd))
}
func read_trampoline()

func pipe() (r, w int32, errno int32) {
	var p [2]int32
	errno = libcCall(unsafe.Pointer(abi.FuncPCABI0(pipe_trampoline)), noescape(unsafe.Pointer(&p)))
	return p[0], p[1], errno
}
func pipe_trampoline()

func closefd(fd int32) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(close_trampoline)), unsafe.Pointer(&fd))
}
func close_trampoline()

// go:nosplit 
// go:cgo_unsafe_args 
// 
// 在运行时/cgo通过链接名导出到程序集。go:nosplit 
func exit(code int32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(exit_trampoline)), unsafe.Pointer(&code))
}
func exit_trampoline()

// go:cgo\u unsafe\u args 
func usleep(usec uint32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
}
func usleep_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func usleep_no_g(usec uint32) {
	asmcgocall_no_g(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
}

// go nosplit 
// go:cgo\u unsafe\u args 
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(write_trampoline)), unsafe.Pointer(&fd))
}
func write_trampoline()

// go
func open(name *byte, mode, perm int32) (ret int32) {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(open_trampoline)), unsafe.Pointer(&name))
}
func open_trampoline()

func nanotime1() int64 {
	var r struct {
		t            int64  // 原始计时器
		numer, denom uint32 // 转换系数。纳秒=t*数字/分米。
	}
	libcCall(unsafe.Pointer(abi.FuncPCABI0(nanotime_trampoline)), unsafe.Pointer(&r))
	// 注意：苹果似乎不关心这里的溢出问题。参见
	// https:
	// 注意，numer==denom==1是常见的。go:cgoèu unsafeèargs 
	t := r.t
	if r.numer != 1 {
		t *= int64(r.numer)
	}
	if r.denom != 1 {
		t /= int64(r.denom)
	}
	return t
}
func nanotime_trampoline()

func walltime() (int64, int32) {
	var t timespec
	libcCall(unsafe.Pointer(abi.FuncPCABI0(walltime_trampoline)), unsafe.Pointer(&t))
	return t.tv_sec, int32(t.tv_nsec)
}
func walltime_trampoline()

// go:nosplit 
// go:cgoèu unsafeèargs 
func sigaction(sig uint32, new *usigactiont, old *usigactiont) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaction_trampoline)), unsafe.Pointer(&sig))
}
func sigaction_trampoline()

// go:nosplit 
// go:cgoèu unsafeèargs 
func sigprocmask(how uint32, new *sigset, old *sigset) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigprocmask_trampoline)), unsafe.Pointer(&how))
}
func sigprocmask_trampoline()

// go:nosplit 
func sigaltstack(new *stackt, old *stackt) {
	if new != nil && new.ss_flags&_SS_DISABLE != 0 && new.ss_size == 0 {
		// 当设置SS_DISABLE时，它不会忽略。sigaltstack返回ENOMEM 
		// 如果我们没有给它一个合理的大小。go:nosplit 
		new.ss_size = 32768
	}
	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaltstack_trampoline)), unsafe.Pointer(&new))
}
func sigaltstack_trampoline()

// go:cgo_unsafe_args 
func raiseproc(sig uint32) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(raiseproc_trampoline)), unsafe.Pointer(&sig))
}
func raiseproc_trampoline()

// go:nosplit 
// go:cgo_unsafe_args 
func setitimer(mode int32, new, old *itimerval) {
	libcCall(unsafe.Pointer(abi.FuncPCABI0(setitimer_trampoline)), unsafe.Pointer(&mode))
}
func setitimer_trampoline()

// go:nosplit 
// go:cgo_unsafe_args 
func sysctl(mib *uint32, miblen uint32, oldp *byte, oldlenp *uintptr, newp *byte, newlen uintptr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(sysctl_trampoline)), unsafe.Pointer(&mib))
}
func sysctl_trampoline()

// go
func sysctlbyname(name *byte, oldp *byte, oldlenp *uintptr, newp *byte, newlen uintptr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(sysctlbyname_trampoline)), unsafe.Pointer(&name))
}
func sysctlbyname_trampoline()

func fcntl(fd, cmd, arg int32) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&fd))
}
func fcntl_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func kqueue() int32 {
	v := libcCall(unsafe.Pointer(abi.FuncPCABI0(kqueue_trampoline)), nil)
	return v
}
func kqueue_trampoline()

// go:nosplit 
// go:cgo\u unsafe\u args 
func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(kevent_trampoline)), unsafe.Pointer(&kq))
}
func kevent_trampoline()

// go:nosplit 
// go:cgo\U unsafe\U args 
func pthread_mutex_init(m *pthreadmutex, attr *pthreadmutexattr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_init_trampoline)), unsafe.Pointer(&m))
}
func pthread_mutex_init_trampoline()

// go:nosplit 
// go:cgo\U unsafe\U args 
func pthread_mutex_lock(m *pthreadmutex) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_lock_trampoline)), unsafe.Pointer(&m))
}
func pthread_mutex_lock_trampoline()

// go nosplit 
// go go cgo\U unsafe\U args 
func pthread_mutex_unlock(m *pthreadmutex) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_mutex_unlock_trampoline)), unsafe.Pointer(&m))
}
func pthread_mutex_unlock_trampoline()

// go nosplit 
func pthread_cond_init(c *pthreadcond, attr *pthreadcondattr) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_init_trampoline)), unsafe.Pointer(&c))
}
func pthread_cond_init_trampoline()

func pthread_cond_wait(c *pthreadcond, m *pthreadmutex) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_wait_trampoline)), unsafe.Pointer(&c))
}
func pthread_cond_wait_trampoline()

// go:cgo\u-unsafe\u-args-
func pthread_cond_timedwait_relative_np(c *pthreadcond, m *pthreadmutex, t *timespec) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_timedwait_relative_np_trampoline)), unsafe.Pointer(&c))
}
func pthread_cond_timedwait_relative_np_trampoline()

// go:nosplit-
// go:cgo\u-unsafe\u-args-
func pthread_cond_signal(c *pthreadcond) int32 {
	return libcCall(unsafe.Pointer(abi.FuncPCABI0(pthread_cond_signal_trampoline)), unsafe.Pointer(&c))
}
func pthread_cond_signal_trampoline()

// 未在达尔文上使用，但必须定义。
func exitThread(wait *uint32) {
}

// go:nosplit 
func closeonexec(fd int32) {
	fcntl(fd, _F_SETFD, _FD_CLOEXEC)
}

// go:nosplit 
func setNonblock(fd int32) {
	flags := fcntl(fd, _F_GETFL, 0)
	fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
}

// 告诉链接器将在系统库中找到libc_*函数
// 但缺少libc_*前缀。

// go:cgo_导入动态libc_pthread_attr_init pthread_attr_init”/usr/lib/libSystem.B.dylib“
// go:cgo_导入动态libc_pthread_attr_getstacksize pthread_attr_getstacksize”/usr/lib/libSystem.B.dylib“
// go:cgo:cgo_导入动态libu pthread_pthread_attr_getstacksize pthread_ptu pth pth pthread sethread state attr\u attr attr settachread attr attr attr detacksize”/ust“/usr/lib/libSystem.B.dylib”
// go:cgo_import_dynamic libc_pthread_create pthread_create/usr/libSystem.B.dylib”
// go:cgo_import_dynamic libc_pthread_self pthread_self”/usr/libSystem.B.dylib”

// go:cgo_import_dynamic libc_read”/usr/lib/libSystem.B.dylib“
// go:cgo_import_dynamic libc_write”/usr/lib/libSystem.B.dylib“
// go:cgo_import_dynamic libc_pipe”/usr/libSystem.B.dylib”

// go:cgo_import_dynamic libc_munmap munmap”/usr/lib/libSystem.B.dylib“
// go:cgo_import_dynamic libc_madvise madvise”/usr/lib/libSystem.B.dylib“
// go:cgo\u import\u dynamic libc\u usleep usleep”/usr/lib/libSystem.B.dylib”

// /go:cgo\u import\u dynamic libc\u mach\u timebase\u info mach\u timebase\u info”/usr/libSystem.B.dylib”
// go:cgo_import_dynamic libc_clock_gettime clock_gettime”/usr/lib/libSystem.B.dylib”
// go:cgo_import_dynamic libc_sigaction sigaction”/usr/lib/libSystem.B.dylib“
// go:cgo_import_dynamic libc_pthread_pthread_sigpthread sigmask”/usr/libSystem.B.dylib
// go:cgo\u import\u dynamic libc\u sigaltstack sigaltstack/usr/lib/libSystem.B.dylib“
// go:cgo\u import\u dynamic libc\u getpid getpid”/usr/lib/libSystem.B.dylib”
// go:cgo_import_dynamic libc_sysctl sysctl/usr/libSystem.B.dylib”
// go:cgo_import_dynamic libc_sysctlbyname/usr/lib/libSystem.B.dylib”

// go:cgo\u import\u dynamic libc\u pthread\u mutex\u lock pthread\u mutex\u lock”/usr/lib/libSystem.B.dylib“
// go:cgo\u import\u dynamic libc\u pthread\u mutex\u unlock pthread\u mutex\u unlock”/usr/libSystem.B.dylib“
// go:cgo:cgo\u import\u dynamic libc\u pthread\u init/lib/lib.B.dynamic lib
// go:cgo\u import\u dynamic libc\u pthread\u cond\u wait pthread\u cond\u wait/usr/lib/libSystem.B.dylib“
// /go:cgo\u import\u dynamic libc\u pthread\u timedwait\u relative\u timedwait\u/usr/libSystem.B.dylib”
